﻿using System;
using System.Web;

using System.Text;
using System.Data;
using System.Xml;
using System.Web.SessionState;
using System.Web.Services;

using Gift.WebApp;
using System.Collections;
using System.IO;

using Gift.DAL;
using Gift.Model;

namespace WebApp.cn.ashx
{
    public class orderInfoHandler : IHttpHandler, IRequiresSessionState
    {

        readonly BasePage thePage = new BasePage();

        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        protected string remindSetXmlPath = "/xml/remind_set.xml";


        DALCard theCard;
        tbOrder oOrder = new tbOrder();
        tbUserAddress oUserAddress = new tbUserAddress();
        tbOrderGoodsEntitys oOrderGoodsEntitys = new tbOrderGoodsEntitys();
        tbGoodsEntitys oGoodsEntitys;

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

            try
            {
                DataTable shoppingCartByUser;
                DataRow current;
                IEnumerator enumerator;
                if (context.Request.Form["region"] != null)
                {
                    StringBuilder builder = new StringBuilder();
                    if (thePage.LoginUserMember != null)
                    {
                        builder.AppendLine("<h6>配送方式：</h6>");
                        builder.AppendLine("<table class=\"secondtable\">");
                        shoppingCartByUser = BaseFactory.theShoppingCart.GetShoppingCartByUser(thePage.LoginUserMember.MemberId);
                        int num = 0;
                        decimal num2 = 0M;
                        int freightTemplate = 0;
                        string item = context.Request.Form["region"];
                        enumerator = shoppingCartByUser.Rows.GetEnumerator();

                        while (enumerator.MoveNext())
                        {
                            current = (DataRow)enumerator.Current;
                            num += Convert.ToInt32(current["amount"]);
                            if (Convert.ToInt32(current["isFromFreight"]) == 0)
                            {
                                DataTable table2 = BaseFactory.theFreightWay.GetFreightWayByFreightTemplate(Convert.ToInt32(current["freightTemplate"]));
                                ArrayList list = new ArrayList();
                                foreach (DataRow row2 in table2.Rows)
                                {
                                    if ((ArrayList.Adapter(row2["region"].ToString().Split(new char[] { ',' })).Contains(item) && !list.Contains(row2["deliveryWayId"].ToString())) && (Convert.ToDecimal(row2["defaultFreight"]) > num2))
                                    {
                                        num2 = Convert.ToDecimal(row2["defaultFreight"]);
                                        freightTemplate = Convert.ToInt32(current["freightTemplate"]);
                                    }
                                }
                                foreach (DataRow row2 in table2.Rows)
                                {
                                    if (((row2["region"].ToString() == "0") && !list.Contains(row2["deliveryWayId"].ToString())) && (Convert.ToDecimal(row2["defaultFreight"]) > num2))
                                    {
                                        num2 = Convert.ToDecimal(row2["defaultFreight"]);
                                        freightTemplate = Convert.ToInt32(current["freightTemplate"]);
                                    }
                                }
                            }
                        }

                        DataTable freightWayByFreightTemplate = BaseFactory.theFreightWay.GetFreightWayByFreightTemplate(freightTemplate);
                        ArrayList list3 = new ArrayList();
                        if (freightWayByFreightTemplate.Rows.Count > 0)
                        {
                            foreach (DataRow row2 in freightWayByFreightTemplate.Rows)
                            {
                                bool flag = false;
                                decimal num4 = 0M;
                                ArrayList list4 = new ArrayList();
                                IEnumerator enumerator2 = shoppingCartByUser.Rows.GetEnumerator();

                                while (enumerator2.MoveNext())
                                {
                                    current = (DataRow)enumerator2.Current;
                                    if (Convert.ToInt32(current["isFromFreight"]) == 0)
                                    {
                                        DataTable table4 = BaseFactory.theFreightWay.GetFreightWayByFreightTemplate(Convert.ToInt32(current["freightTemplate"]));
                                        foreach (DataRow row3 in table4.Rows)
                                        {
                                            if (Convert.ToInt32(row3["deliveryWayId"]) == Convert.ToInt32(row2["deliveryWayId"]))
                                            {
                                                if ((ArrayList.Adapter(row3["region"].ToString().Split(new char[] { ',' })).Contains(item) && !list3.Contains(row2["deliveryWayId"].ToString())) && !list4.Contains(Convert.ToInt32(current["geid"])))
                                                {
                                                    if (Convert.ToInt32(row3["freightTemplateId"]) == Convert.ToInt32(row2["freightTemplateId"]))
                                                    {
                                                        if (flag)
                                                        {
                                                            num4 += Convert.ToDecimal(row3["increasedFreight"]) * Convert.ToInt32(current["amount"]);
                                                        }
                                                        else
                                                        {
                                                            num4 += Convert.ToDecimal(row3["defaultFreight"]) + (Convert.ToDecimal(row3["increasedFreight"]) * (Convert.ToInt32(current["amount"]) - 1));
                                                            flag = true;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        num4 += Convert.ToDecimal(row3["increasedFreight"]) * Convert.ToInt32(current["amount"]);
                                                    }
                                                    list4.Add(Convert.ToInt32(current["geid"]));
                                                }
                                                if (((row3["region"].ToString() == "0") && !list3.Contains(row2["deliveryWayId"].ToString())) && !list4.Contains(Convert.ToInt32(current["geid"])))
                                                {
                                                    if (Convert.ToInt32(row3["freightTemplateId"]) == Convert.ToInt32(row2["freightTemplateId"]))
                                                    {
                                                        if (flag)
                                                        {
                                                            num4 += Convert.ToDecimal(row3["increasedFreight"]) * Convert.ToInt32(current["amount"]);
                                                        }
                                                        else
                                                        {
                                                            num4 += Convert.ToDecimal(row3["defaultFreight"]) + (Convert.ToDecimal(row3["increasedFreight"]) * (Convert.ToInt32(current["amount"]) - 1));
                                                            flag = true;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        num4 += Convert.ToDecimal(row3["increasedFreight"]) * Convert.ToInt32(current["amount"]);
                                                    }
                                                    list4.Add(Convert.ToInt32(current["geid"]));
                                                }
                                            }
                                        }
                                    }
                                }

                                if (num4 != 0M)
                                {
                                    list3.Add(row2["deliveryWayId"].ToString());
                                    builder.AppendLine("<tr>");
                                    builder.AppendLine(string.Concat(new object[] { "<td class=\"firsttd\"><input type=\"radio\" name=\"deliveryWay\" onclick=\"changeDeliveryWay('", num4, "');\" value=\"", row2["deliveryWay"], ",", num4, "\" />", row2["deliveryWay"], "</td>" }));
                                    builder.AppendLine("<td><strong>+ " + string.Format("{0:C}", num4) + "</strong></td>");
                                    builder.AppendLine("</tr>");
                                }
                            }
                        }
                        else
                        {
                            builder.AppendLine("<tr>");
                            builder.AppendLine("<td class=\"firsttd\"><input type=\"radio\" name=\"deliveryWay\" onclick=\"changeDeliveryWay('0');\" value=\"0,0\" />卖家承担运费 </td>");
                            builder.AppendLine("<td><strong>+ 0.00</strong></td>");
                            builder.AppendLine("</tr>");
                        }
                        builder.AppendLine("</table>");
                    }
                    context.Response.Write(builder.ToString());
                }
                if ((((context.Request.Form["userAddress"] != null) && (context.Request.Form["deliveryWay"] != null)) && ((context.Request.Form["pay_bank"] != null) && (context.Request.Form["postscript"] != null))) && (context.Request.Form["goodsSum"] != null))
                {
                    if (Convert.ToInt32(context.Request.Form["userAddress"]) == 0)
                    {
                        if (Convert.ToInt32(context.Request.Form["isSave"]) == 1 && thePage.LoginUserMember.IsCard == false)
                        {
                            this.oUserAddress.user = thePage.LoginUserMember.ClientUser.id;
                            this.oUserAddress.consignee = context.Request.Form["consignee"];
                            this.oUserAddress.region = context.Request.Form["provinces"] + ((context.Request.Form["city"] == "") ? "" : ("," + context.Request.Form["city"]));
                            this.oUserAddress.address = context.Request.Form["address"];
                            this.oUserAddress.zipcode = context.Request.Form["zipCode"];
                            this.oUserAddress.telephone = context.Request.Form["telephone"];
                            this.oUserAddress.mobile = context.Request.Form["mobile"];
                            this.oUserAddress.isDefault = false;
                            BaseFactory.theUserAddress.Add(this.oUserAddress);
                            this.oOrder.consignee = context.Request.Form["consignee"];
                            this.oOrder.region = context.Request.Form["provinces"] + "," + context.Request.Form["city"];
                            this.oOrder.address = context.Request.Form["address"];
                            this.oOrder.zipcode = context.Request.Form["zipCode"];
                            this.oOrder.telephone = context.Request.Form["telephone"];
                            this.oOrder.mobile = context.Request.Form["mobile"];
                        }
                        else
                        {
                            this.oOrder.consignee = context.Request.Form["consignee"];
                            this.oOrder.region = context.Request.Form["provinces"] + "," + context.Request.Form["city"];
                            this.oOrder.address = context.Request.Form["address"];
                            this.oOrder.zipcode = context.Request.Form["zipCode"];
                            this.oOrder.telephone = context.Request.Form["telephone"];
                            this.oOrder.mobile = context.Request.Form["mobile"];
                        }
                    }
                    else
                    {
                        oUserAddress = BaseFactory.theUserAddress.GetModel(Convert.ToInt32(context.Request.Form["userAddress"]));
                        if (oUserAddress != null)
                        {

                            this.oOrder.consignee = oUserAddress.consignee;
                            this.oOrder.region = oUserAddress.region;
                            this.oOrder.address = oUserAddress.address;
                            this.oOrder.zipcode = oUserAddress.zipcode;
                            this.oOrder.telephone = oUserAddress.telephone;
                            this.oOrder.mobile = oUserAddress.mobile;
                        }
                    }
                    this.oOrder.orderNo = "PM_" + DateTime.Now.ToString("yyyyMMddHHmmssffffff");
                    this.oOrder.orderUser = thePage.LoginUserMember.ClientUser == null ? 0 : thePage.LoginUserMember.ClientUser.id;
                    this.oOrder.orderStatus = 0;
                    this.oOrder.deliveryStatus = 0;
                    this.oOrder.payStatus = 0;
                    this.oOrder.postscript = context.Request.Form["postscript"];
                    this.oOrder.goodsTotal = Convert.ToDecimal(context.Request.Form["goodsSum"]);
                    this.oOrder.orderTotal = Convert.ToDecimal(context.Request.Form["deliveryWay"].Split(new char[] { ',' }).GetValue(1)) + Convert.ToDecimal(context.Request.Form["goodsSum"]);
                    this.oOrder.deliveryWay = (context.Request.Form["deliveryWay"].Split(new char[] { ',' }).GetValue(0).ToString() == "0") ? "卖家承担运费" : context.Request.Form["deliveryWay"].Split(new char[] { ',' }).GetValue(0).ToString();
                    this.oOrder.deliveryMoney = Convert.ToDecimal(context.Request.Form["deliveryWay"].Split(new char[] { ',' }).GetValue(1));
                    int num5 = 0;
                    int num6 = 0;
                    decimal num7 = 0M;
                    decimal num8 = 0M;
                    decimal num9 = 0M;
                    decimal num10 = 0M;
                    decimal num11 = 0M;
                    decimal num12 = 0M;
                    decimal num13 = 0M;
                    decimal num14 = 0M;
                    decimal num15 = 0M;
                    decimal num16 = 0M;
                    decimal num17 = 0M;
                    decimal num18 = 0M;
                    decimal num19 = 0M;
                    string str2 = "";
                    DataTable table6 = BaseFactory.theShoppingCart.GetShoppingCartByUser(thePage.LoginUserMember.MemberId);
                    enumerator = table6.Rows.GetEnumerator();

                    while (enumerator.MoveNext())
                    {
                        current = (DataRow)enumerator.Current;
                        if (Convert.ToInt32(current["isOrderFromFreight"]) == 1)
                        {
                            num8 += Convert.ToDecimal(current["total"]);
                        }
                        else
                        {
                            num7 += Convert.ToDecimal(current["total"]);
                        }
                        if (Convert.ToInt32(current["isDiscount"]) == 1)
                        {
                            num10 += Convert.ToDecimal(current["total"]);
                        }
                        else
                        {
                            num9 += Convert.ToDecimal(current["total"]);
                        }
                        if (Convert.ToInt32(current["isTotalReduce"]) == 1)
                        {
                            num12 += Convert.ToDecimal(current["total"]);
                        }
                        else
                        {
                            num11 += Convert.ToDecimal(current["total"]);
                        }
                        if (Convert.ToInt32(current["isGifts"]) == 1)
                        {
                            num14 += Convert.ToDecimal(current["total"]);
                        }
                        else
                        {
                            num13 += Convert.ToDecimal(current["total"]);
                        }
                        if (Convert.ToInt32(current["isSendIntegral"]) == 1)
                        {
                            num16 += Convert.ToDecimal(current["total"]);
                        }
                        else
                        {
                            num5 += Convert.ToInt32(current["giveIntegral"]) * Convert.ToInt32(current["amount"]);
                            num15 += Convert.ToDecimal(current["total"]);
                        }
                        num6 += Convert.ToInt32(current["amount"]);
                        num19 += Convert.ToDecimal(current["total"]);
                    }

                    DataTable goodsPromotion = BaseFactory.theGoodsPromotion.GetList("").Tables[0];
                    enumerator = goodsPromotion.Rows.GetEnumerator();

                    while (enumerator.MoveNext())
                    {
                        current = (DataRow)enumerator.Current;
                        if (Convert.ToInt32(current["promotionType"]) == 1)
                        {
                            if (((num8 >= Convert.ToDecimal(current["floorAmount"])) && (DateTime.Now >= Convert.ToDateTime(current["startTime"]))) && (DateTime.Now <= Convert.ToDateTime(current["endTime"])))
                            {
                                str2 = str2 + "<br />&nbsp;" + current["promotionTitle"].ToString();
                                this.oOrder.deliveryWay = "卖家承担运费";
                                this.oOrder.deliveryMoney = 0M;
                            }
                        }
                        else
                        {
                            string str4;
                            if (Convert.ToInt32(current["promotionType"]) == 2)
                            {
                                if ((((num10 >= Convert.ToDecimal(current["floorAmount"])) && (num10 < Convert.ToDecimal(current["limitAmount"]))) && (DateTime.Now >= Convert.ToDateTime(current["startTime"]))) && (DateTime.Now <= Convert.ToDateTime(current["endTime"])))
                                {
                                    num17 = num10 * (Convert.ToDecimal(1) - (Convert.ToDecimal(current["discount"]) / 100M));
                                    str4 = str2;
                                    str2 = str4 + "<br />&nbsp;" + current["promotionTitle"].ToString() + "，减" + string.Format("{0:C}", num17);
                                }
                            }
                            else if (Convert.ToInt32(current["promotionType"]) == 3)
                            {
                                if ((((num12 >= Convert.ToDecimal(current["floorAmount"])) && (num12 < Convert.ToDecimal(current["limitAmount"]))) && (DateTime.Now >= Convert.ToDateTime(current["startTime"]))) && (DateTime.Now <= Convert.ToDateTime(current["endTime"])))
                                {
                                    num18 = Convert.ToDecimal(current["preferentialAmount"]);
                                    str4 = str2;
                                    str2 = str4 + "<br />&nbsp;" + current["promotionTitle"].ToString() + "，减" + string.Format("{0:C}", num18);
                                }
                            }
                            else if (Convert.ToInt32(current["promotionType"]) == 4)
                            {
                                if ((((num14 >= Convert.ToDecimal(current["floorAmount"])) && (num14 < Convert.ToDecimal(current["limitAmount"]))) && (DateTime.Now >= Convert.ToDateTime(current["startTime"]))) && (DateTime.Now <= Convert.ToDateTime(current["endTime"])))
                                {
                                    str2 = str2 + "<br />&nbsp;" + current["promotionTitle"].ToString();
                                }
                            }
                            else if ((Convert.ToInt32(current["promotionType"]) == 5) && ((((num16 >= Convert.ToDecimal(current["floorAmount"])) && (num16 < Convert.ToDecimal(current["limitAmount"]))) && (DateTime.Now >= Convert.ToDateTime(current["startTime"]))) && (DateTime.Now <= Convert.ToDateTime(current["endTime"]))))
                            {
                                num5 += Convert.ToInt32(current["presentedIntegral"]);
                                if (Convert.ToInt32(current["isOverlap"]) == 1)
                                {
                                    foreach (DataRow row5 in table6.Rows)
                                    {
                                        if (Convert.ToInt32(row5["isSendIntegral"]) == 1)
                                        {
                                            num5 += Convert.ToInt32(row5["giveIntegral"]) * Convert.ToInt32(row5["amount"]);
                                        }
                                    }
                                }
                                str2 = str2 + "<br />&nbsp;" + current["promotionTitle"].ToString();
                            }
                        }
                    }

                    this.oOrder.goodsTotal = (num19 - num17) - num18;
                    this.oOrder.orderTotal = this.oOrder.goodsTotal + this.oOrder.deliveryMoney;
                    this.oOrder.presentedIntegra = num5;
                    this.oOrder.postscript = context.Request.Form["postscript"];
                    this.oOrder.orderRemark = this.oOrder.orderRemark + ((str2 != "") ? ("参加促销活动：" + str2) : "");
                    if (((context.Request.Form["pay_bank"] != "货到付款")
                        && (context.Request.Form["pay_bank"] != "卡券支付")
                        && (context.Request.Form["pay_bank"] != "预存款"))
                        && (context.Request.Form["pay_bank"] != "Paypal"))
                    {
                        this.oOrder.payment = "支付宝";
                    }
                    else
                    {
                        this.oOrder.payment = context.Request.Form["pay_bank"];
                    }
                    this.oOrder.payBank = context.Request.Form["pay_bank"];
                    bool isUpdateStates = false;
                    if (thePage.LoginUserMember.IsCard)
                    {
                        decimal dTotalCardPrice = 0;
                        foreach (tbCard card in thePage.LoginUserMember.DicUserCardList.Values)
                        {

                            dTotalCardPrice += card.cardprice;
                        }
                        if (dTotalCardPrice != this.oOrder.goodsTotal)
                        {
                            context.Response.Write("1");
                            return;
                        }

                        tbCard tempCard;
                        string sCardCodes = "";
                        long orderId = new DALOrderAccount().GenerateOrderCodeForBefor();
                        try
                        {
                            if (thePage.LoginUserMember.DicUserCardList != null)
                            {
                                tbCar oCar = new tbCar();
                                DataTable tbShoppingCart = BaseFactory.theShoppingCart.GetShoppingCartByUser(thePage.LoginUserMember.MemberId);
                                if (tbShoppingCart == null || tbShoppingCart.Rows.Count <= 0)
                                {
                                }
                                DALCardOrder theCardOrder = new DALCardOrder();



                                foreach (DataRow dr in tbShoppingCart.Rows)
                                {

                                    this.oOrderGoodsEntitys = new tbOrderGoodsEntitys();
                                    this.oOrderGoodsEntitys.orderNo = orderId.ToString();
                                    this.oOrderGoodsEntitys.goodsEntitys = Convert.ToInt32(dr["geid"]);
                                    this.oOrderGoodsEntitys.price = Convert.ToDecimal(dr["salePrice"]);
                                    this.oOrderGoodsEntitys.amount = Convert.ToInt32(dr["amount"]);
                                    BaseFactory.theOrderGoodsEntitys.Add(this.oOrderGoodsEntitys);


                                    oCar.orderId = orderId;
                                    oCar.ProdNumber = int.Parse(dr["amount"].ToString());
                                    oGoodsEntitys = BaseFactory.theGoodsEntitys.GetModel(int.Parse(dr["geid"].ToString()));
                                    oCar.ProdPrice = oGoodsEntitys.salePrice;
                                    oCar.ProductId = oGoodsEntitys.goods;
                                    oCar.UpdateDate = DateTime.Now;
                                    BaseFactory.theCar.Add(oCar);
                                }

                                foreach (tbCard card in thePage.LoginUserMember.DicUserCardList.Values)
                                {
                                    sCardCodes += card.cardcode + "|";
                                    theCard = new DALCard(card.cardcode.Substring(0, 4));
                                    tempCard = theCard.GetModel(card.cardid);
                                    tempCard.states = 2;

                                    theCard.Update(tempCard);
                                }
                                tbCardOrder oCardOrder = new tbCardOrder();
                                oCardOrder.orderid = orderId;
                                oCardOrder.address = oOrder.address;
                                oCardOrder.address1 = oOrder.address;
                                oCardOrder.cardprice = oOrder.goodsTotal;
                                oCardOrder.cname = oOrder.consignee;
                                oCardOrder.cname1 = oOrder.consignee;
                                oCardOrder.ordertime = DateTime.Now;
                                oCardOrder.pay = (int)Gift.Model.ENUM.EPayType.Card;
                                oCardOrder.phone = oOrder.mobile;
                                oCardOrder.phone1 = oOrder.mobile;
                                oCardOrder.tel = oOrder.telephone;
                                oCardOrder.tel1 = oOrder.telephone;
                                oCardOrder.totalprice = oOrder.goodsTotal.ToString();
                                oCardOrder.post = oOrder.zipcode;
                                oCardOrder.post1 = oOrder.zipcode;
                                oCardOrder.CardCodes = sCardCodes;
                                oCardOrder.content = oOrder.postscript;
                                oCardOrder.Senddate = DateTime.Now.AddDays(3);
                                BaseFactory.theCar.AddTempToReal(oCardOrder.orderid);
                                theCardOrder.Add(oCardOrder);
                                thePage.LoginUserMember = null;
                                isUpdateStates = true;
                                context.Response.Write(oCardOrder.orderid.ToString());
                                return;
                            }
                        }
                        catch
                        {
                            BaseFactory.theOrderGoodsEntitys.DeleteByOrderNo(orderId.ToString());
                            BaseFactory.theCar.DeleteByOrderId(orderId.ToString());
                            foreach (tbCard card in thePage.LoginUserMember.DicUserCardList.Values)
                            {
                                sCardCodes += "'" + card.cardcode + "',";
                                theCard = new DALCard(card.cardcode.Substring(0, 4));
                                tempCard = theCard.GetModel(card.cardid);
                                tempCard.states = 1;
                                theCard.Update(tempCard);
                            }
                        }
                    }
                    else
                    {
                        if (BaseFactory.theOrder.Add(this.oOrder) > 0)
                        {
                            isUpdateStates = true;
                        }
                    }
                    if (isUpdateStates && thePage.LoginUserMember != null && thePage.LoginUserMember.ClientUser != null)
                    {
                        SendMailMessage message;
                        string str3;
                        Random random;
                        XmlDocument document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                        XmlElement element = document.SelectSingleNode("set") as XmlElement;
                        XmlElement element2 = document.SelectSingleNode("set").ChildNodes[1] as XmlElement;
                        if (element2.GetAttribute("mail_remind") == "true")
                        {
                            if (thePage.LoginUserMember.ClientUser != null && thePage.LoginUserMember.ClientUser.email != null)
                            {
                                message = new SendMailMessage();
                                str3 = File.ReadAllText(context.Server.MapPath("/cn/txt_template/order_submit.txt"));
                                message.subject = "订单提交成功";
                                message.body = str3.Replace("[userName]", thePage.LoginUserMember.ClientUser.userName).Replace("[dateTime]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("[orderNo]", this.oOrder.orderNo).Replace("[orderTotal]", string.Format("{0:C}", this.oOrder.orderTotal));
                                message.html = true;
                                shoppingCartByUser = BaseFactory.theMail.GetAllEnabledMail();
                                random = new Random();
                                current = shoppingCartByUser.Rows[random.Next(shoppingCartByUser.Rows.Count - 1)];
                                message.from = current["fromAddress"].ToString();
                                message.fromName = current["fromName"].ToString();
                                message.mailDomain = current["mailDomain"].ToString();
                                message.mailDomainPort = Convert.ToInt32(current["mailDomainPort"]);
                                message.mailServerUserName = current["mailServerUserName"].ToString();
                                message.mailServerPassWord = current["mailServerPassWord"].ToString();
                                if (element2.GetAttribute("notice_member") == "true")
                                {
                                    message.mailTo = thePage.LoginUserMember.ClientUser.email;
                                    //  message.RuleSend();
                                }
                                if (element2.GetAttribute("notice_admin") == "true")
                                {
                                    message.mailTo = element.GetAttribute("remind_email");
                                    //message.RuleSend();
                                }
                            }
                        }
                        this.oOrderGoodsEntitys.orderNo = this.oOrder.orderNo;
                        DataTable table8 = BaseFactory.theShoppingCart.GetShoppingCartByUser(thePage.LoginUserMember.MemberId);
                        foreach (DataRow row in table8.Rows)
                        {
                            this.oOrderGoodsEntitys = new tbOrderGoodsEntitys();
                            this.oOrderGoodsEntitys.goodsEntitys = Convert.ToInt32(row["geid"]);
                            this.oOrderGoodsEntitys.price = Convert.ToDecimal(row["salePrice"]);
                            this.oOrderGoodsEntitys.amount = Convert.ToInt32(row["amount"]);
                            if (BaseFactory.theOrderGoodsEntitys.Add(this.oOrderGoodsEntitys) > 0)
                            {
                                BaseFactory.theShoppingCart.Delete(Convert.ToInt32(row["sid"]));
                                BaseFactory.theGoodsEntitys.ModifyGoodsEntitysInventory(this.oOrderGoodsEntitys.goodsEntitys, this.oOrderGoodsEntitys.amount);
                                DataTable goodsEntitysById = BaseFactory.theGoodsEntitys.GetGoodsEntitysById(this.oOrderGoodsEntitys.goodsEntitys);
                                if (goodsEntitysById.Rows.Count > 0)
                                {
                                    DataTable goodsById = BaseFactory.theGoods.GetGoodsById(Convert.ToInt32(goodsEntitysById.Rows[0]["goods"]));
                                    if (Convert.ToInt32(goodsEntitysById.Rows[0]["inventory"]) == 0)
                                    {
                                        DataRow row6 = goodsById.Rows[0];
                                        if (element2.GetAttribute("mail_remind") == "true")
                                        {
                                            message = new SendMailMessage();
                                            str3 = File.ReadAllText(context.Server.MapPath("/cn/txt_template/inventory_warning.txt"));
                                            message.subject = "库存警告";
                                            message.body = str3.Replace("[gooodsNo]", row6["goodsNo"].ToString()).Replace("[goodsArtno]", goodsEntitysById.Rows[0]["goodsArtno"].ToString());
                                            message.html = true;
                                            shoppingCartByUser = BaseFactory.theMail.GetAllEnabledMail();
                                            random = new Random();
                                            message.from = row["fromAddress"].ToString();
                                            message.fromName = row["fromName"].ToString();
                                            message.mailDomain = row["mailDomain"].ToString();
                                            message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                                            message.mailServerUserName = row["mailServerUserName"].ToString();
                                            message.mailServerPassWord = row["mailServerPassWord"].ToString();
                                            if (element2.GetAttribute("notice_member") == "true")
                                            {
                                                message.mailTo = thePage.LoginUserMember.ClientUser.email;
                                                message.RuleSend();
                                            }
                                            if (element2.GetAttribute("notice_admin") == "true")
                                            {
                                                message.mailTo = element.GetAttribute("remind_email");
                                                message.RuleSend();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        context.Response.Write(this.oOrder.orderNo);
                    }
                    else
                    {
                        context.Response.Write("0");
                    }
                }
            }
            catch
            {
                context.Response.Write("0");
            }
        }

        #endregion
    }
}
